package com.heima.behavior.service.impl;


import com.alibaba.fastjson.JSON;
import com.heima.behavior.dtos.ReadBehaviorDto;
import com.heima.behavior.pojos.ApReadBehavior;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.mess.UpdateArticleMess;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 阅读行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {

        //1.参数判断
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取用户id
        ApUser apUser = ApThreadLocal.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (apUser.getId() == 0) {
            return ResponseResult.okResult(405, "游客身份禁止，需进行登录");
        }
        //3.判断当前mongo中是否已经包含了该数据，如果包含则更新count次数，如果没有包含则直接新增
        ApReadBehavior apReadBehavior = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(apUser.getId()).and("articleId").is(dto.getArticleId())), ApReadBehavior.class);
        if (apReadBehavior != null) {//说明已经存在该数据，则更新count次数
            apReadBehavior.setCount(apReadBehavior.getCount() + 1);
            apReadBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apReadBehavior);
        } else {//说明没有数据，直接新增
            apReadBehavior = new ApReadBehavior();
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setCreatedTime(new Date());
            apReadBehavior.setUpdatedTime(new Date());
            apReadBehavior.setUserId(apUser.getId().longValue());
            apReadBehavior.setCount(dto.getCount().intValue());

            mongoTemplate.insert(apReadBehavior);

//            apReadBehavior.setArticleId(dto.getArticleId());
//            apReadBehavior.setCreatedTime(new Date());
//            apReadBehavior.setUpdatedTime(new Date());
//            apReadBehavior.setUserId(apUser.getId().longValue());
//            apReadBehavior.setCount(dto.getCount().intValue());
        }

        //封装发送消息的内容
        UpdateArticleMess mess=new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setAdd(1);
        //发送消息内容给kafka
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
        return ResponseResult.okResult("阅读完成");
    }
}
